<?php
namespace models\service;
//use entities;

class KatalogService {

	protected $ci;
	
	public function __construct() {
		$this -> ci = &get_instance();
	}

	public function paginateBarang($indexAndCount, $orders) {
		return $this -> ci -> em -> getRepository('\models\entities\Barang') -> paginateBarang($indexAndCount, $orders);
	}

	public function paginateBarangByMap($map, $indexAndCount, $orders) {
		return $this -> ci -> em -> getRepository('\models\entities\Barang') -> paginateBarangByMap($map, $indexAndCount, $orders);
	}
	
	public function paginateBarangItemByMap($map, $indexAndCount, $orders) {
		return $this -> ci -> em -> getRepository('\models\entities\BarangItem') -> paginateByMap($map, $indexAndCount, $orders);
	}
		
	public function getBarangById($id){
		$criteria = array('id' => $id);
		$barang = $this -> ci -> em -> getRepository('\models\entities\Barang') -> findOneBy($criteria);
		return $barang; 
	}
	
	public function saveBarang($param) {		
		if ($param != null) {
			if ($param['Barang']['id'] != "0") {
				$criteria = array('id' => $param['Barang']['id']);
				$barang = $this -> ci -> em -> getRepository('\models\entities\Barang') -> findOneBy($criteria);
			} else {
				$barang = new \models\entities\Barang;
				if ($param['Barang']['barcode'] != NULL){
					$barang->setBarcode($param['Barang']['barcode']);
				} else {
					$barcode = generateCode(1,'',6);
					$barang->setBarcode($barcode[0]);
				}
				if ($param['BarangStock']['price'] != NULL){
					$barangStock = new \models\entities\BarangStock;
					$barangStock->setPrice($param['BarangStock']['price']);	
					$barangStock->setCreatedDate(new \DateTime('now', new \DateTimeZone("Asia/Jakarta")));
					$barangStock->setStockDate(new \DateTime($param['BarangStock']['stockDate'],new \DateTimeZone("Asia/Jakarta")));
					//$barangStock->setUserInput($param['BarangStock']['userInput']);					
				}
				if ($param['BarangPrice']['price'] != NULL){
					$barangPrice = new \models\entities\BarangPrice;
					$barangPrice->setPrice($param['BarangPrice']['price']);
					$barangPrice->setCreatedDate(new \DateTime('now', new \DateTimeZone("Asia/Jakarta")));
					//$barangPrice->setUserInput($param['BarangPrice']['userInput']);
				}
			}
			$barang->setName($param['Barang']['name']);
			$barang->setDescription($param['Barang']['description']);
			$barang->setUnit($param['Barang']['unit']);
			$barang->setColor($param['Barang']['color']);
			$barang->setQty($param['Barang']['qty']);
			$createdDate = new \DateTime('now', new \DateTimeZone("Asia/Jakarta"));
			$barang -> setCreatedDate($createdDate);
			
			
			if (array_key_exists("category",$param['Barang'])){
				$cat = $param['Barang']['category'] != "" ? $param['Barang']['category'] : "";
				if ($cat != ""){
					$cat = $this -> ci -> em -> getRepository('\models\entities\Category') -> findOneBy(array('id'=>$cat));
					$barang->setCategory($cat);
				} 
			}
			
			$this->ci->em->getConnection()->beginTransaction(); // suspend auto-commit
			try {			    
			    $this->ci->em->persist($barang);
			    $this->ci->em->flush();
				if ($param['Barang']['id'] == "0") {
					$barangPrice->setBarang($barang);
					$this->ci->em->persist($barangPrice);
				    $this->ci->em->flush();
					$barangStock->setBarang($barang);
					$this->ci->em->persist($barangStock);
				    $this->ci->em->flush();
				}
				$this->ci->em->getConnection()->commit();
				$this -> barangLastId = $barang -> getId();
			} catch (Exception $e) {
			    $this->ci->em->getConnection()->rollback();
			    $this->ci->em->close();
			    throw $e;
				return FALSE;
			}			
			
			return TRUE;
		} else {
			return FALSE;
		}
	}

	
	
}
